home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / u_man / cat1 / perlref.z / perlref
Text File  |  1998-10-30  |  27KB  |  727 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlref - Perl references and nested data structures
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      Before release 5 of Perl it was difficult to represent complex data
  13.      structures, because all references had to be symbolic, and even that was
  14.      difficult to do when you wanted to refer to a variable rather than a
  15.      symbol table entry.  Perl not only makes it easier to use symbolic
  16.      references to variables, but lets you have "hard" references to any piece
  17.      of data.  Any scalar may hold a hard reference.  Because arrays and
  18.      hashes contain scalars, you can now easily build arrays of arrays, arrays
  19.      of hashes, hashes of arrays, arrays of hashes of functions, and so on.
  20.  
  21.      Hard references are smart--they keep track of reference counts for you,
  22.      automatically freeing the thing referred to when its reference count goes
  23.      to zero.  (Note: The reference counts for values in self-referential or
  24.      cyclic data structures may not go to zero without a little help; see the
  25.      section on _T_w_o-_P_h_a_s_e_d _G_a_r_b_a_g_e _C_o_l_l_e_c_t_i_o_n in the _p_e_r_l_o_b_j manpage for a
  26.      detailed explanation.  If that thing happens to be an object, the object
  27.      is destructed.  See the _p_e_r_l_o_b_j manpage for more about objects.  (In a
  28.      sense, everything in Perl is an object, but we usually reserve the word
  29.      for references to objects that have been officially "blessed" into a
  30.      class package.)
  31.  
  32.      Symbolic references are names of variables or other objects, just as a
  33.      symbolic link in a Unix filesystem contains merely the name of a file.
  34.      The *glob notation is a kind of symbolic reference.  (Symbolic references
  35.      are sometimes called "soft references", but please don't call them that;
  36.      references are confusing enough without useless synonyms.)
  37.  
  38.      In contrast, hard references are more like hard links in a Unix file
  39.      system: They are used to access an underlying object without concern for
  40.      what its (other) name is.  When the word "reference" is used without an
  41.      adjective, like in the following paragraph, it usually is talking about a
  42.      hard reference.
  43.  
  44.      References are easy to use in Perl.  There is just one overriding
  45.      principle: Perl does no implicit referencing or dereferencing.  When a
  46.      scalar is holding a reference, it always behaves as a simple scalar.  It
  47.      doesn't magically start being an array or hash or subroutine; you have to
  48.      tell it explicitly to do so, by dereferencing it.
  49.  
  50.      References can be constructed in several ways.
  51.  
  52.      1.  By using the backslash operator on a variable, subroutine, or value.
  53.          (This works much like the & (address-of) operator in C.)  Note that
  54.          this typically creates _A_N_O_T_H_E_R reference to a variable, because
  55.          there's already a reference to the variable in the symbol table.  But
  56.          the symbol table reference might go away, and you'll still have the
  57.          reference that the backslash returned.  Here are some examples:
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  71.  
  72.  
  73.  
  74.              $scalarref = \$foo;
  75.              $arrayref  = \@ARGV;
  76.              $hashref   = \%ENV;
  77.              $coderef   = \&handler;
  78.              $globref   = \*foo;
  79.  
  80.          It isn't possible to create a true reference to an IO handle
  81.          (filehandle or dirhandle) using the backslash operator.  See the
  82.          explanation of the *foo{THING} syntax below.  (However, you're apt to
  83.          find Perl code out there using globrefs as though they were IO
  84.          handles, which is grandfathered into continued functioning.)
  85.  
  86.      2.  A reference to an anonymous array can be constructed using square
  87.          brackets:
  88.  
  89.              $arrayref = [1, 2, ['a', 'b', 'c']];
  90.  
  91.          Here we've constructed a reference to an anonymous array of three
  92.          elements whose final element is itself a reference to another
  93.          anonymous array of three elements.  (The multidimensional syntax
  94.          described later can be used to access this.  For example, after the
  95.          above, $arrayref->[2][1] would have the value "b".)
  96.  
  97.          Note that taking a reference to an enumerated list is not the same as
  98.          using square brackets--instead it's the same as creating a list of
  99.          references!
  100.  
  101.              @list = (\$a, \@b, \%c);
  102.              @list = \($a, @b, %c);      # same thing!
  103.  
  104.          As a special case, \(@foo) returns a list of references to the
  105.          contents of @foo, not a reference to @foo itself.  Likewise for %foo.
  106.  
  107.      3.  A reference to an anonymous hash can be constructed using curly
  108.          brackets:
  109.  
  110.              $hashref = {
  111.                  'Adam'  => 'Eve',
  112.                  'Clyde' => 'Bonnie',
  113.              };
  114.  
  115.          Anonymous hash and array constructors can be intermixed freely to
  116.          produce as complicated a structure as you want.  The multidimensional
  117.          syntax described below works for these too.  The values above are
  118.          literals, but variables and expressions would work just as well,
  119.          because assignment operators in Perl (even within _l_o_c_a_l() or _m_y())
  120.          are executable statements, not compile-time declarations.
  121.  
  122.          Because curly brackets (braces) are used for several other things
  123.          including BLOCKs, you may occasionally have to disambiguate braces at
  124.          the beginning of a statement by putting a + or a return in front so
  125.          that Perl realizes the opening brace isn't starting a BLOCK.  The
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  137.  
  138.  
  139.  
  140.          economy and mnemonic value of using curlies is deemed worth this
  141.          occasional extra hassle.
  142.  
  143.          For example, if you wanted a function to make a new hash and return a
  144.          reference to it, you have these options:
  145.  
  146.              sub hashem {        { @_ } }   # silently wrong
  147.              sub hashem {       +{ @_ } }   # ok
  148.              sub hashem { return { @_ } }   # ok
  149.  
  150.  
  151.      4.  A reference to an anonymous subroutine can be constructed by using
  152.          sub without a subname:
  153.  
  154.              $coderef = sub { print "Boink!\n" };
  155.  
  156.          Note the presence of the semicolon.  Except for the fact that the
  157.          code inside isn't executed immediately, a sub {} is not so much a
  158.          declaration as it is an operator, like do{} or eval{}.  (However, no
  159.          matter how many times you execute that line (unless you're in an
  160.          eval("...")), $coderef will still have a reference to the _S_A_M_E
  161.          anonymous subroutine.)
  162.  
  163.          Anonymous subroutines act as closures with respect to _m_y() variables,
  164.          that is, variables visible lexically within the current scope.
  165.          Closure is a notion out of the Lisp world that says if you define an
  166.          anonymous function in a particular lexical context, it pretends to
  167.          run in that context even when it's called outside of the context.
  168.  
  169.          In human terms, it's a funny way of passing arguments to a subroutine
  170.          when you define it as well as when you call it.  It's useful for
  171.          setting up little bits of code to run later, such as callbacks.  You
  172.          can even do object-oriented stuff with it, though Perl already
  173.          provides a different mechanism to do that--see the _p_e_r_l_o_b_j manpage.
  174.  
  175.          You can also think of closure as a way to write a subroutine template
  176.          without using eval.  (In fact, in version 5.000, eval was the _o_n_l_y
  177.          way to get closures.  You may wish to use "require 5.001" if you use
  178.          closures.)
  179.  
  180.          Here's a small example of how closures works:
  181.  
  182.              sub newprint {
  183.                  my $x = shift;
  184.                  return sub { my $y = shift; print "$x, $y!\n"; };
  185.              }
  186.              $h = newprint("Howdy");
  187.              $g = newprint("Greetings");
  188.  
  189.              # Time passes...
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  203.  
  204.  
  205.  
  206.              &$h("world");
  207.              &$g("earthlings");
  208.  
  209.          This prints
  210.  
  211.              Howdy, world!
  212.              Greetings, earthlings!
  213.  
  214.          Note particularly that $x continues to refer to the value passed into
  215.          _n_e_w_p_r_i_n_t() _d_e_s_p_i_t_e the fact that the "my $x" has seemingly gone out
  216.          of scope by the time the anonymous subroutine runs.  That's what
  217.          closure is all about.
  218.  
  219.          This applies to only lexical variables, by the way.  Dynamic
  220.          variables continue to work as they have always worked.  Closure is
  221.          not something that most Perl programmers need trouble themselves
  222.          about to begin with.
  223.  
  224.      5.  References are often returned by special subroutines called
  225.          constructors.  Perl objects are just references to a special kind of
  226.          object that happens to know which package it's associated with.
  227.          Constructors are just special subroutines that know how to create
  228.          that association.  They do so by starting with an ordinary reference,
  229.          and it remains an ordinary reference even while it's also being an
  230.          object.  Constructors are customarily named _n_e_w(), but don't have to
  231.          be:
  232.  
  233.              $objref = new Doggie (Tail => 'short', Ears => 'long');
  234.  
  235.  
  236.      6.  References of the appropriate type can spring into existence if you
  237.          dereference them in a context that assumes they exist.  Because we
  238.          haven't talked about dereferencing yet, we can't show you any
  239.          examples yet.
  240.  
  241.      7.  A reference can be created by using a special syntax, lovingly known
  242.          as the *foo{THING} syntax.  *foo{THING} returns a reference to the
  243.          THING slot in *foo (which is the symbol table entry which holds
  244.          everything known as foo).
  245.  
  246.              $scalarref = *foo{SCALAR};
  247.              $arrayref  = *ARGV{ARRAY};
  248.              $hashref   = *ENV{HASH};
  249.              $coderef   = *handler{CODE};
  250.              $ioref     = *STDIN{IO};
  251.              $globref   = *foo{GLOB};
  252.  
  253.          All of these are self-explanatory except for *foo{IO}.  It returns
  254.          the IO handle, used for file handles (the open entry in the _p_e_r_l_f_u_n_c
  255.          manpage), sockets (the socket entry in the _p_e_r_l_f_u_n_c manpage and the
  256.          socketpair entry in the _p_e_r_l_f_u_n_c manpage), and directory handles (the
  257.          opendir entry in the _p_e_r_l_f_u_n_c manpage).  For compatibility with
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  269.  
  270.  
  271.  
  272.          previous versions of Perl, *foo{FILEHANDLE} is a synonym for
  273.          *foo{IO}.
  274.  
  275.          *foo{THING} returns undef if that particular THING hasn't been used
  276.          yet, except in the case of scalars.  *foo{SCALAR} returns a reference
  277.          to an anonymous scalar if $foo hasn't been used yet.  This might
  278.          change in a future release.
  279.  
  280.          The use of *foo{IO} is the best way to pass bareword filehandles into
  281.          or out of subroutines, or to store them in larger data structures.
  282.  
  283.              splutter(*STDOUT{IO});
  284.              sub splutter {
  285.                  my $fh = shift;
  286.                  print $fh "her um well a hmmm\n";
  287.              }
  288.  
  289.              $rec = get_rec(*STDIN{IO});
  290.              sub get_rec {
  291.                  my $fh = shift;
  292.                  return scalar <$fh>;
  293.              }
  294.  
  295.          Beware, though, that you can't do this with a routine which is going
  296.          to open the filehandle for you, because *HANDLE{IO} will be undef if
  297.          HANDLE hasn't been used yet.  Use \*HANDLE for that sort of thing
  298.          instead.
  299.  
  300.          Using \*HANDLE (or *HANDLE) is another way to use and store non-
  301.          bareword filehandles (before perl version 5.002 it was the only way).
  302.          The two methods are largely interchangeable, you can do
  303.  
  304.              splutter(\*STDOUT);
  305.              $rec = get_rec(\*STDIN);
  306.  
  307.          with the above subroutine definitions.
  308.  
  309.      That's it for creating references.  By now you're probably dying to know
  310.      how to use references to get back to your long-lost data.  There are
  311.      several basic methods.
  312.  
  313.      1.  Anywhere you'd put an identifier (or chain of identifiers) as part of
  314.          a variable or subroutine name, you can replace the identifier with a
  315.          simple scalar variable containing a reference of the correct type:
  316.  
  317.              $bar = $$scalarref;
  318.              push(@$arrayref, $filename);
  319.              $$arrayref[0] = "January";
  320.              $$hashref{"KEY"} = "VALUE";
  321.              &$coderef(1,2,3);
  322.              print $globref "output\n";
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  335.  
  336.  
  337.  
  338.          It's important to understand that we are specifically _N_O_T
  339.          dereferencing $arrayref[0] or $hashref{"KEY"} there.  The dereference
  340.          of the scalar variable happens _B_E_F_O_R_E it does any key lookups.
  341.          Anything more complicated than a simple scalar variable must use
  342.          methods 2 or 3 below.  However, a "simple scalar" includes an
  343.          identifier that itself uses method 1 recursively.  Therefore, the
  344.          following prints "howdy".
  345.  
  346.              $refrefref = \\\"howdy";
  347.              print $$$$refrefref;
  348.  
  349.  
  350.      2.  Anywhere you'd put an identifier (or chain of identifiers) as part of
  351.          a variable or subroutine name, you can replace the identifier with a
  352.          BLOCK returning a reference of the correct type.  In other words, the
  353.          previous examples could be written like this:
  354.  
  355.              $bar = ${$scalarref};
  356.              push(@{$arrayref}, $filename);
  357.              ${$arrayref}[0] = "January";
  358.              ${$hashref}{"KEY"} = "VALUE";
  359.              &{$coderef}(1,2,3);
  360.              $globref->print("output\n");  # iff IO::Handle is loaded
  361.  
  362.          Admittedly, it's a little silly to use the curlies in this case, but
  363.          the BLOCK can contain any arbitrary expression, in particular,
  364.          subscripted expressions:
  365.  
  366.              &{ $dispatch{$index} }(1,2,3);      # call correct routine
  367.  
  368.          Because of being able to omit the curlies for the simple case of $$x,
  369.          people often make the mistake of viewing the dereferencing symbols as
  370.          proper operators, and wonder about their precedence.  If they were,
  371.          though, you could use parentheses instead of braces.  That's not the
  372.          case.  Consider the difference below; case 0 is a short-hand version
  373.          of case 1, _N_O_T case 2:
  374.  
  375.              $$hashref{"KEY"}   = "VALUE";       # CASE 0
  376.              ${$hashref}{"KEY"} = "VALUE";       # CASE 1
  377.              ${$hashref{"KEY"}} = "VALUE";       # CASE 2
  378.              ${$hashref->{"KEY"}} = "VALUE";     # CASE 3
  379.  
  380.          Case 2 is also deceptive in that you're accessing a variable called
  381.          %hashref, not dereferencing through $hashref to the hash it's
  382.          presumably referencing.  That would be case 3.
  383.  
  384.      3.  Subroutine calls and lookups of individual array elements arise often
  385.          enough that it gets cumbersome to use method 2.  As a form of
  386.          syntactic sugar, the examples for method 2 may be written:
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  401.  
  402.  
  403.  
  404.              $arrayref->[0] = "January";   # Array element
  405.              $hashref->{"KEY"} = "VALUE";  # Hash element
  406.              $coderef->(1,2,3);            # Subroutine call
  407.  
  408.          The left side of the arrow can be any expression returning a
  409.          reference, including a previous dereference.  Note that $array[$x] is
  410.          _N_O_T the same thing as $array->[$x] here:
  411.  
  412.              $array[$x]->{"foo"}->[0] = "January";
  413.  
  414.          This is one of the cases we mentioned earlier in which references
  415.          could spring into existence when in an lvalue context.  Before this
  416.          statement, $array[$x] may have been undefined.  If so, it's
  417.          automatically defined with a hash reference so that we can look up
  418.          {"foo"} in it.  Likewise $array[$x]->{"foo"} will automatically get
  419.          defined with an array reference so that we can look up [0] in it.
  420.  
  421.          One more thing here.  The arrow is optional _B_E_T_W_E_E_N brackets
  422.          subscripts, so you can shrink the above down to
  423.  
  424.              $array[$x]{"foo"}[0] = "January";
  425.  
  426.          Which, in the degenerate case of using only ordinary arrays, gives
  427.          you multidimensional arrays just like C's:
  428.  
  429.              $score[$x][$y][$z] += 42;
  430.  
  431.          Well, okay, not entirely like C's arrays, actually.  C doesn't know
  432.          how to grow its arrays on demand.  Perl does.
  433.  
  434.      4.  If a reference happens to be a reference to an object, then there are
  435.          probably methods to access the things referred to, and you should
  436.          probably stick to those methods unless you're in the class package
  437.          that defines the object's methods.  In other words, be nice, and
  438.          don't violate the object's encapsulation without a very good reason.
  439.          Perl does not enforce encapsulation.  We are not totalitarians here.
  440.          We do expect some basic civility though.
  441.  
  442.      The _r_e_f() operator may be used to determine what type of thing the
  443.      reference is pointing to.  See the _p_e_r_l_f_u_n_c manpage.
  444.  
  445.      The _b_l_e_s_s() operator may be used to associate a reference with a package
  446.      functioning as an object class.  See the _p_e_r_l_o_b_j manpage.
  447.  
  448.      A typeglob may be dereferenced the same way a reference can, because the
  449.      dereference syntax always indicates the kind of reference desired.  So
  450.      ${*foo} and ${\$foo} both indicate the same scalar variable.
  451.  
  452.      Here's a trick for interpolating a subroutine call into a string:
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  467.  
  468.  
  469.  
  470.          print "My sub returned @{[mysub(1,2,3)]} that time.\n";
  471.  
  472.      The way it works is that when the @{...} is seen in the double-quoted
  473.      string, it's evaluated as a block.  The block creates a reference to an
  474.      anonymous array containing the results of the call to mysub(1,2,3).  So
  475.      the whole block returns a reference to an array, which is then
  476.      dereferenced by @{...} and stuck into the double-quoted string. This
  477.      chicanery is also useful for arbitrary expressions:
  478.  
  479.          print "That yields @{[$n + 5]} widgets\n";
  480.  
  481.  
  482.      SSSSyyyymmmmbbbboooolllliiiicccc rrrreeeeffffeeeerrrreeeennnncccceeeessss
  483.  
  484.      We said that references spring into existence as necessary if they are
  485.      undefined, but we didn't say what happens if a value used as a reference
  486.      is already defined, but _I_S_N'_T a hard reference.  If you use it as a
  487.      reference in this case, it'll be treated as a symbolic reference.  That
  488.      is, the value of the scalar is taken to be the _N_A_M_E of a variable, rather
  489.      than a direct link to a (possibly) anonymous value.
  490.  
  491.      People frequently expect it to work like this.  So it does.
  492.  
  493.          $name = "foo";
  494.          $$name = 1;                 # Sets $foo
  495.          ${$name} = 2;               # Sets $foo
  496.          ${$name x 2} = 3;           # Sets $foofoo
  497.          $name->[0] = 4;             # Sets $foo[0]
  498.          @$name = ();                # Clears @foo
  499.          &$name();                   # Calls &foo() (as in Perl 4)
  500.          $pack = "THAT";
  501.          ${"${pack}::$name"} = 5;    # Sets $THAT::foo without eval
  502.  
  503.      This is very powerful, and slightly dangerous, in that it's possible to
  504.      intend (with the utmost sincerity) to use a hard reference, and
  505.      accidentally use a symbolic reference instead.  To protect against that,
  506.      you can say
  507.  
  508.          use strict 'refs';
  509.  
  510.      and then only hard references will be allowed for the rest of the
  511.      enclosing block.  An inner block may countermand that with
  512.  
  513.          no strict 'refs';
  514.  
  515.      Only package variables are visible to symbolic references.  Lexical
  516.      variables (declared with _m_y()) aren't in a symbol table, and thus are
  517.      invisible to this mechanism.  For example:
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  533.  
  534.  
  535.  
  536.          local($value) = 10;
  537.          $ref = \$value;
  538.          {
  539.              my $value = 20;
  540.              print $$ref;
  541.          }
  542.  
  543.      This will still print 10, not 20.  Remember that _l_o_c_a_l() affects package
  544.      variables, which are all "global" to the package.
  545.  
  546.      NNNNooootttt----ssssoooo----ssssyyyymmmmbbbboooolllliiiicccc rrrreeeeffffeeeerrrreeeennnncccceeeessss
  547.  
  548.      A new feature contributing to readability in perl version 5.001 is that
  549.      the brackets around a symbolic reference behave more like quotes, just as
  550.      they always have within a string.  That is,
  551.  
  552.          $push = "pop on ";
  553.          print "${push}over";
  554.  
  555.      has always meant to print "pop on over", despite the fact that push is a
  556.      reserved word.  This has been generalized to work the same outside of
  557.      quotes, so that
  558.  
  559.          print ${push} . "over";
  560.  
  561.      and even
  562.  
  563.          print ${ push } . "over";
  564.  
  565.      will have the same effect.  (This would have been a syntax error in Perl
  566.      5.000, though Perl 4 allowed it in the spaceless form.)  Note that this
  567.      construct is _n_o_t considered to be a symbolic reference when you're using
  568.      strict refs:
  569.  
  570.          use strict 'refs';
  571.          ${ bareword };      # Okay, means $bareword.
  572.          ${ "bareword" };    # Error, symbolic reference.
  573.  
  574.      Similarly, because of all the subscripting that is done using single
  575.      words, we've applied the same rule to any bareword that is used for
  576.      subscripting a hash.  So now, instead of writing
  577.  
  578.          $array{ "aaa" }{ "bbb" }{ "ccc" }
  579.  
  580.      you can write just
  581.  
  582.          $array{ aaa }{ bbb }{ ccc }
  583.  
  584.      and not worry about whether the subscripts are reserved words.  In the
  585.      rare event that you do wish to do something like
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  599.  
  600.  
  601.  
  602.          $array{ shift }
  603.  
  604.      you can force interpretation as a reserved word by adding anything that
  605.      makes it more than a bareword:
  606.  
  607.          $array{ shift() }
  608.          $array{ +shift }
  609.          $array{ shift @_ }
  610.  
  611.      The ----wwww switch will warn you if it interprets a reserved word as a string.
  612.      But it will no longer warn you about using lowercase words, because the
  613.      string is effectively quoted.
  614.  
  615. WWWWAAAARRRRNNNNIIIINNNNGGGG
  616.      You may not (usefully) use a reference as the key to a hash.  It will be
  617.      converted into a string:
  618.  
  619.          $x{ \$a } = $a;
  620.  
  621.      If you try to dereference the key, it won't do a hard dereference, and
  622.      you won't accomplish what you're attempting.  You might want to do
  623.      something more like
  624.  
  625.          $r = \@a;
  626.          $x{ $r } = $r;
  627.  
  628.      And then at least you can use the _v_a_l_u_e_s(), which will be real refs,
  629.      instead of the _k_e_y_s(), which won't.
  630.  
  631. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  632.      Besides the obvious documents, source code can be instructive.  Some
  633.      rather pathological examples of the use of references can be found in the
  634.      _t/_o_p/_r_e_f._t regression test in the Perl source directory.
  635.  
  636.      See also the _p_e_r_l_d_s_c manpage and the _p_e_r_l_l_o_l manpage for how to use
  637.      references to create complex data structures, and the _p_e_r_l_o_b_j manpage for
  638.      how to use them to create objects.
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))                                                          PPPPEEEERRRRLLLLRRRREEEEFFFF((((1111))))
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.                                                                        PPPPaaaaggggeeee 11111111
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.